package cn.dkdoo.utils.config.rollbackData;

import cn.dkdoo.utils.common.ReflectPool;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * @author nicky
 * @date 2021/5/10 下午3:42
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RollbackDataSelf {
    private static final Logger logger = LoggerFactory.getLogger(RollbackDataSelf.class);
    /**
     * 回滚时候，反射调用的bean
     * todo-nicky 减少存储数据
     */
    private String beanName;

    /**
     * 回滚时候，反射调用的methodName
     */
    private String methodName;

    /**
     * 回滚时候，反射调用的method的传参
     */
    private Collection<?> args;


    /**
     * 回滚时候，反射调用的method的传参-class
     * 目的：处理同名重载方法
     */
    //private Collection<?> argClassName;

    /**
     * todo-nicky 存储的时候只存id,转换的时候根据id找到对应的method
     * 目的：减少存储
     */
    private String methodId;

    public RollbackDataSelf(String beanName, String methodName, Collection<?> args) {
        //fixme methodName+beanName 必须要传，因为可能重启应用，导致缓存清空，需要重新反射循环去找到目标方法
        this.beanName = beanName;
        this.methodName = methodName;
        this.args = args;
        try {
            this.methodId = ReflectPool.addMethod(beanName, methodName, args);
        } catch (NoSuchMethodException ex) {
            logger.error("new RollbackData error", ex);
            //throw new RuntimeException(ex);
        }
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Collection<?> getArgs() {
        return args;
    }

    public void setArgs(Collection<?> args) {
        this.args = args;
    }

    public String getMethodId() {
        return methodId;
    }

    public void setMethodId(String methodId) {
        this.methodId = methodId;
    }
}
